import _objectSpread from "@babel/runtime/helpers/esm/objectSpread2";
import _classCallCheck from "@babel/runtime/helpers/esm/classCallCheck";
import _createClass from "@babel/runtime/helpers/esm/createClass";
import _inherits from "@babel/runtime/helpers/esm/inherits";
import _createSuper from "@babel/runtime/helpers/esm/createSuper";
import _applyDecoratedDescriptor from "@babel/runtime/helpers/esm/applyDecoratedDescriptor";

var _dec, _dec2, _dec3, _dec4, _dec5, _dec6, _class;

/**
 * HistoryData
 * @date 2019/9/29
 */
import React, { Component, createRef } from 'react';
import { Modal } from 'antd';
import { Bind } from 'lodash-decorators';
import intl from "../../../utils/intl";
import notification from "../../../utils/notification";
import { createPagination, getCurrentOrganizationId, getRefFormData, getResponse } from "../../../utils/utils";
import { getEnvConfig } from "../../../utils/iocUtils";
import { downloadFile, queryIdpValue } from 'cpaas-front/lib/services/api';
import FilterForm from "./FilterForm";
import DataTable from "./DataTable";
import { historyDataCancel, historyDataQuery } from "./historyDataService";

var _getEnvConfig = getEnvConfig(),
    BKT_PLATFORM = _getEnvConfig.BKT_PLATFORM,
    CPAAS_FILE = _getEnvConfig.CPAAS_FILE;

var HistoryData = (_dec = Bind(), _dec2 = Bind(), _dec3 = Bind(), _dec4 = Bind(), _dec5 = Bind(), _dec6 = Bind(), (_class = /*#__PURE__*/function (_Component) {
  _inherits(HistoryData, _Component);

  var _super = _createSuper(HistoryData);

  function HistoryData(props) {
    var _this;

    _classCallCheck(this, HistoryData);

    _this = _super.call(this, props);
    _this.state = {
      typeList: [],
      // 任务状态
      errorInfoModalVisible: false,
      // 错误信息展示弹窗的 页面
      errorInfo: '' // 错误信息

    };
    _this.filterFormRef = createRef();
    return _this;
  }

  _createClass(HistoryData, [{
    key: "componentDidMount",
    value: function componentDidMount() {
      var _this2 = this;

      queryIdpValue('CPFM.ASYNC.TASK.STATE').then(function (res) {
        var typeList = getResponse(res);

        if (typeList) {
          _this2.setState({
            typeList: typeList
          });
        }
      });
      this.handleSearch();
    } // base

  }, {
    key: "handleSearch",
    value: function handleSearch() {
      var _this3 = this;

      var pagination = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {};
      var params = getRefFormData(this.filterFormRef);
      this.setState({
        queryLoading: true
      }, function () {
        historyDataQuery(_objectSpread(_objectSpread({}, params), pagination)).then(function (dataSourceRes) {
          var dataSource = getResponse(dataSourceRes);

          if (dataSource) {
            _this3.setState({
              dataSource: dataSource.content,
              pagination: createPagination(dataSource),
              cachePagination: pagination,
              queryLoading: false,
              cancelLoading: false
            });
          } else {
            _this3.setState({
              queryLoading: false,
              cancelLoading: false
            });
          }
        }, function () {
          _this3.setState({
            queryLoading: false,
            cancelLoading: false
          });
        });
      });
    }
  }, {
    key: "reload",
    value: function reload() {
      var _this$state$cachePagi = this.state.cachePagination,
          cachePagination = _this$state$cachePagi === void 0 ? {} : _this$state$cachePagi;
      this.handleSearch(cachePagination);
    } // FilterForm

  }, {
    key: "handleFilterFormSearch",
    value: function handleFilterFormSearch() {
      this.handleSearch();
    } // DataTable

  }, {
    key: "handleTableChange",
    value: function handleTableChange(page, filter, sort) {
      this.handleSearch({
        page: page,
        sort: sort
      });
    }
    /**
     * 显示错误信息
     * @param {string} errorInfo
     */

  }, {
    key: "handleShowErrorInfo",
    value: function handleShowErrorInfo(errorInfo) {
      this.setState({
        errorInfoModalVisible: true,
        errorInfo: errorInfo
      });
    }
  }, {
    key: "handleRecordDownload",
    value: function handleRecordDownload(record) {
      var api = "".concat(CPAAS_FILE, "/v1/").concat(getCurrentOrganizationId(), "/files/download");
      var queryParams = [{
        name: 'url',
        value: encodeURIComponent(record.downloadUrl)
      }];
      queryParams.push({
        name: 'bucketName',
        value: BKT_PLATFORM
      });
      queryParams.push({
        name: 'directory',
        value: 'cpfm01'
      });
      downloadFile({
        requestUrl: api,
        queryParams: queryParams
      });
    }
  }, {
    key: "handleRecordCancel",
    value: function handleRecordCancel(record) {
      var _this4 = this;

      historyDataCancel({
        taskCode: decodeURIComponent(record.taskCode)
      }).then(function (res) {
        if (getResponse(res)) {
          notification.success();

          _this4.reload();
        } else {
          _this4.setState({
            cancelLoading: false
          });
        }
      }, function () {
        _this4.setState({
          cancelLoading: false
        });
      });
    } // ErrorInfo Modal

  }, {
    key: "hideErrorInfoModal",
    value: function hideErrorInfoModal() {
      this.setState({
        errorInfo: '',
        errorInfoModalVisible: false
      });
    }
  }, {
    key: "render",
    value: function render() {
      var _this$state = this.state,
          typeList = _this$state.typeList,
          dataSource = _this$state.dataSource,
          pagination = _this$state.pagination,
          _this$state$errorInfo = _this$state.errorInfo,
          errorInfo = _this$state$errorInfo === void 0 ? '' : _this$state$errorInfo,
          _this$state$errorInfo2 = _this$state.errorInfoModalVisible,
          errorInfoModalVisible = _this$state$errorInfo2 === void 0 ? false : _this$state$errorInfo2,
          queryLoading = _this$state.queryLoading,
          cancelLoading = _this$state.cancelLoading;
      return /*#__PURE__*/React.createElement(React.Fragment, null, /*#__PURE__*/React.createElement(FilterForm, {
        wrappedComponentRef: this.filterFormRef,
        onSearch: this.handleFilterFormSearch,
        typeList: typeList
      }), /*#__PURE__*/React.createElement(DataTable, {
        dataSource: dataSource,
        pagination: pagination,
        onChange: this.handleTableChange,
        onShowErrorInfo: this.handleShowErrorInfo,
        onRecordDownload: this.handleRecordDownload,
        onRecordCancel: this.handleRecordCancel,
        loading: queryLoading || cancelLoading
      }), /*#__PURE__*/React.createElement(Modal, {
        title: intl.get('cpaas.common.component.excelExport.v.hd.errorInfo').d('异常信息'),
        visible: errorInfoModalVisible,
        onCancel: this.hideErrorInfoModal,
        footer: null,
        wrapClassName: "ant-modal-sidebar-right",
        transitionName: "move-right"
      }, errorInfo));
    }
  }]);

  return HistoryData;
}(Component), (_applyDecoratedDescriptor(_class.prototype, "handleFilterFormSearch", [_dec], Object.getOwnPropertyDescriptor(_class.prototype, "handleFilterFormSearch"), _class.prototype), _applyDecoratedDescriptor(_class.prototype, "handleTableChange", [_dec2], Object.getOwnPropertyDescriptor(_class.prototype, "handleTableChange"), _class.prototype), _applyDecoratedDescriptor(_class.prototype, "handleShowErrorInfo", [_dec3], Object.getOwnPropertyDescriptor(_class.prototype, "handleShowErrorInfo"), _class.prototype), _applyDecoratedDescriptor(_class.prototype, "handleRecordDownload", [_dec4], Object.getOwnPropertyDescriptor(_class.prototype, "handleRecordDownload"), _class.prototype), _applyDecoratedDescriptor(_class.prototype, "handleRecordCancel", [_dec5], Object.getOwnPropertyDescriptor(_class.prototype, "handleRecordCancel"), _class.prototype), _applyDecoratedDescriptor(_class.prototype, "hideErrorInfoModal", [_dec6], Object.getOwnPropertyDescriptor(_class.prototype, "hideErrorInfoModal"), _class.prototype)), _class));
export { HistoryData as default };